package com.erb.service.zwj.impl;

import com.erb.constants.AdminRedpacketConstants;
import com.erb.entity.Pager;
import com.erb.entity.Redenvelopes;
import com.erb.mapper.AdminRedpacketMapper;
import com.erb.service.zwj.IAdminRedpacketService;
import com.erb.utils.RedeemCodeUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Transactional
@Service("adminRedpacketService")
public class AdminRedpacketServiceImpl implements IAdminRedpacketService {
	@Resource
	private AdminRedpacketMapper adminRedpacketMapper;

	@Override
	public List<Redenvelopes> findAll(Pager pager) {
		return adminRedpacketMapper.selectAll(pager);
	}

	@Override
	public Redenvelopes findRedpacket(Integer id) {
		if (!ObjectUtils.isEmpty(id)) {
			return adminRedpacketMapper.selectRedpacketById(id);
		}
		return null;
	}

	@Override
	public boolean removeRedpacket(Integer id) {
		if (!ObjectUtils.isEmpty(id) && id > 0) {
			return adminRedpacketMapper.deleteRedpacket(id) > 0;
		}
		return false;
	}

	@Override
	public boolean saveRedpacket(Redenvelopes redenvelopes) {
		if (ObjectUtils.isEmpty(redenvelopes)) {
			return false;
		}

		if (!ObjectUtils.isEmpty(redenvelopes.getRtype())) {// 设置红包类型
			if (redenvelopes.getRtype() <= AdminRedpacketConstants.REDPACKET_RTYPE_NO) {
				// 无需门槛金额与项目类型
				redenvelopes.setThreshold(null);
				redenvelopes.setPtype(null);
			} else if (redenvelopes.getRtype().equals(AdminRedpacketConstants.REDPACKET_RTYPE_FULL)) {
				// 无需项目类型
				redenvelopes.setPtype(null);
			} else if (redenvelopes.getRtype().equals(AdminRedpacketConstants.REDPACKET_RTYPE_PTYPE)) {
				// 无需门槛金额
				redenvelopes.setThreshold(null);
			}
		}

		// 设置新增红包的类型，红包有效
		redenvelopes.setState(AdminRedpacketConstants.REDPACKET_STATE_VALID);
		// 生成兑换码
		redenvelopes.setExchangeCode(RedeemCodeUtils.generateCode(AdminRedpacketConstants.REDPACKET_CODE_LENGTH));

		return adminRedpacketMapper.insertRedpacket(redenvelopes) > 0;
	}

	@Override
	public boolean checkStateForEndTime() {
		return adminRedpacketMapper.updateStateForEndTime() > 0;
	}

	/**
	 * 通过兑换码查询对象
	 *
	 * @param redeemCode
	 * @return
	 */
	@Override
	public Redenvelopes findRedpacketByRedeemCode(String redeemCode) {
		if (ObjectUtils.isEmpty(redeemCode)) {
			return null;
		}
		Redenvelopes redenvelopes = adminRedpacketMapper.selectRedpacketByRedeemCode(redeemCode);

		return ObjectUtils.isEmpty(redenvelopes) ? null : redenvelopes;
	}

	/**
	 * 通过ID更改信息：红包名称，红包介绍，门槛金额，抵扣金额，红包个数
	 *
	 * @param redenvelopes
	 * @return
	 */
	@Override
	public boolean modifyRedpacket(Redenvelopes redenvelopes) {
		if (ObjectUtils.isEmpty(redenvelopes) || ObjectUtils.isEmpty(redenvelopes.getId())) {
			return false;
		}

		return adminRedpacketMapper.updateRedpacket(redenvelopes) > 0;
	}

	/**
	 * 通过ID增量红包个数
	 *
	 * @param id
	 * @param increment
	 * @return
	 */
	@Override
	public boolean incrementNumber(int id, int increment) {
		if (id <= 0) return false;

		// 判断红包是否过期
		Redenvelopes redpacket = findRedpacket(id);
		if (redpacket.getState().equals(AdminRedpacketConstants.REDPACKET_STATE_INVALID)) {
			// 红包已过期
			return false;
		}

		// 增量
		redpacket.setNumber(redpacket.getNumber() + increment);

		// 更新数据库
		return this.modifyRedpacket(redpacket);
	}
}
